<?php

namespace app\common\model\app;

use app\common\library\Auth;
use think\Cache;
use think\Model;
use think\Log;

class Intel extends Model
{
    // 表名
    protected $name = 'app_intel';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'rtype_text',
        'column_text',
        'verify_text',
        'verifytime_text',
        'mediatype_text',
        'recommendswitch_text'
    ];

    protected static function init()
    {
        self::beforeDelete(function ($row) {
            // 减话题请报数
            if ($row['topic_ids']) {
                // 实例化redis by Fire
                $options = config('cache');
                $options['prefix'] = 'gfw_topic';
                $options['select'] = 5;
                $redis = Cache::connect($options);
                $topic_ids = explode(',', $row['topic_ids']);
                foreach ($topic_ids as $k => $v) {
                    if ($v) {
                        $redis->dec($v.'_countIntel');
                    }
                }
            }
        });

        self::afterDelete(function ($row) {
            // 删除图片
            $arr = model("app.IntelAttachment")->where('intel_id', $row->id)->select();
            if ($arr) {
                foreach ($arr as $kk => $vv) {
                    model("app.IntelAttachment")->where('id', $vv['id'])->delete();
                    $attachmentFile = ROOT_PATH . '/public/uploads' . $vv['url'];
                    if (is_file($attachmentFile)) {
                        @unlink($attachmentFile);
                    }
                    //同步删除oss
                    \think\Hook::listen("upload_delete", $vv);
                }
            }
        });
    }

    public function getMediatypeList()
    {
        return ['0' => __('Mediatype 0'), '1' => __('Mediatype 1'), '2' => __('Mediatype 2')];
    }

    public function getRecommendswitchList()
    {
        return ['0' => __('Recommendswitch 0'), '1' => __('Recommendswitch 1')];
    }

    public function getRtypeList()
    {
        return ['0' => __('Rtype 0'), '1' => __('Rtype 1'), '2' => __('Rtype 2'), '3' => __('Rtype 3'), '4' => __('Rtype 4')];
    }

    public function getColumnList()
    {
        return ['0' => __('Column 0'), '1' => __('Column 1'), '2' => __('Column 2'), '3' => __('Column 3')];
    }

    public function getVerifyList()
    {
        return ['0' => __('Verify 0'), '1' => __('Verify 1'), '2' => __('Verify 2')];
    }

    public function getMediatypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['mediatype']) ? $data['mediatype'] : '');
        $list = $this->getMediatypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getRecommendswitchTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['recommendswitch']) ? $data['recommendswitch'] : '');
        $list = $this->getRecommendswitchList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getRtypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['rtype']) ? $data['rtype'] : '');
        $list = $this->getRtypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getColumnTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['column']) ? $data['column'] : '');
        $list = $this->getColumnList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getVerifyTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['verify']) ? $data['verify'] : '');
        $list = $this->getVerifyList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getVerifytimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['verifytime']) ? $data['verifytime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setVerifytimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function applabel()
    {
        return $this->belongsTo('AppLabel', 'label_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function user()
    {
        return $this->belongsTo('app\common\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function usergroup()
    {
        return $this->hasOne('app\common\model\User', 'id', 'user_id');
        // return $this->hasOne('UserGroup', 'id', 1);
    }

}
